package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrderStatisticsDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.TurnoverItemDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {

    /**
     * 插入订单表
     * @param orders
     */
    void insert(Orders orders);


    /**
     * 根据userId和number修改orders信息
     * @param orders
     */
    @Update("update orders set status = #{status}, checkout_time = #{checkoutTime}, pay_method = #{payMethod}, pay_status = #{payStatus}" +
            " where user_id = #{userId} and number = #{number}")
    void updateOrders(Orders orders);

    /**
     * 分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 更新订单状态
     * @param orders
     */
    @Update("update orders set status = #{status}, pay_status = #{payStatus}, cancel_reason = #{cancelReason}, rejection_reason = #{rejectionReason}, cancel_time = #{cancelTime}" +
            " where id = #{id}")
    void update(Orders orders);

    /**
     * 统计订单状态数据
     * @return
     */
    OrderStatisticsVO getStatistics();

    /**
     * 跟新订单状态
     * @param orders
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void updateStatus(Orders orders);

    /**
     * 查询超时订单
     * @param status
     * @param time
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusAndTimeLT(Integer status, LocalDateTime time);

    /**
     * 跟新超时订单
     * @param orders
     */
    @Update("update orders set status = #{status}, cancel_reason = #{cancelReason}, cancel_time = #{cancelTime} where id = #{id}")
    void updateOrdersLT(Orders orders);

    /**
     * 在规定日期内查询已完成的订单
     * @param beginTime
     * @param endTime
     * @param complete
     * @return
     */

    List<TurnoverItemDTO> getByDateAndStatus(LocalDateTime beginTime, LocalDateTime endTime, Integer complete);

    /**
     * 订单统计
     * @param beginTime
     * @param endTime
     * @return
     */
    List<OrderStatisticsDTO> getOrderStatisticsByDateRange(LocalDateTime beginTime, LocalDateTime endTime);

    /**
     * 获取当前begin的订单总量
     * @param begin
     * @return
     */
    @Select("select COUNT(*) from orders where DATE(order_time) = #{begin}")
    int getTotalByBegin(LocalDate begin);

    /**
     * 获取当前begin的订单有效量
     * @param begin
     * @return
     */
    @Select("select COUNT(*) from orders where DATE(order_time) = #{begin} and status = #{status}")
    int getValidByBegin(LocalDate begin, Integer status);

    /**
     * 查询top10
     * @param beginTime
     * @param endTime
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime beginTime, LocalDateTime endTime);

    /**
     * 根据条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 根据条件统计订单数量
     * @param map
     * @return
     */
    @Select("select SUM(amount) from orders where order_time between #{begin} and #{end}")
    Double sumByMap(Map map);
}
